/**
 * Search clearable field
 *
 * @param store
 *            Datastore
 * @param paramName
 *            Datastore baseParams property
 * @param filterName
 *            ['filter','like'] default 'like'
 */
CQCIS.form.SearchField = Ext.extend(Ext.form.TwinTriggerField, {
    initComponent: function(){
        if (this.store) {
            this.ds = this.store;
            delete this.store;
        }
        CQCIS.form.SearchField.superclass.initComponent.call(this);
        this.on('specialkey', function(f, e){
            if (e.getKey() == e.ENTER) {
                this.onTrigger2Click();
            }
        }, this);
    },
    
    validationEvent: false,
    validateOnBlur: false,
    trigger1Class: 'x-form-clear-trigger',
    trigger2Class: 'x-form-search-trigger',
    hideTrigger1: true,
    width: 100,
    hasSearch: false,
    filterName: 'like',
    paramName: 'query',
    
    onTrigger1Click: function(){
        if (this.hasSearch) {
            this.el.dom.value = '';
            var o = {
                start: 0
            };
            this.ds.baseParams = this.ds.baseParams || {};
            if (this.ds.baseParams[this.filterName]) {
                delete this.ds.baseParams[this.filterName][this.paramName];
            }
            this.ds.reload({
                params: o
            });
            this.triggers[0].hide();
            this.hasSearch = false;
        }
    },
    
    onTrigger2Click: function(){
        var v = this.getRawValue();
        if (v.length < 1) {
            this.onTrigger1Click();
            return;
        }
        var o = {
            start: 0
        };
        this.ds.baseParams = this.ds.baseParams || {};
        this.ds.baseParams[this.filterName] = this.ds.baseParams[this.filterName] || {};
        this.ds.baseParams[this.filterName][this.paramName] = v;
        this.ds.reload({
            params: o
        });
        this.hasSearch = true;
        this.triggers[0].show();
    }
});
